package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.Dialog.ModalExclusionType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenuItem;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.Models.ClassSettingsModel;
import pl.edu.pw.MidEmu.Logic.ApplicationService;
import pl.edu.pw.MidEmu.UI.Components.ClassLoaderFrame;
import pl.edu.pw.MidEmu.UI.Views.ApplicationsListView;

/**
 * Controller for managing application lists
 * @author Mateusz Starodab
 *
 */
public class ApplicationsListController extends BaseController<ApplicationsListView> implements ActionListener, MouseListener
{
	private ApplicationService appService;

	private ClassSettingsModel selectedModel = null;

	public ApplicationsListController()
	{
		appService = new ApplicationService();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.ApplicationList_LoadButton)
			{
				getView().showFileDialog();
			}
			else if (button.getName() == Consts.ApplicationList_RunButton)
			{
				try
				{
					if (getView().getSelectedModel() != null)
					{
						ClassLoaderFrame classLoaderFrame = new ClassLoaderFrame(getView().getSelectedModel(), getView().getParent(), getView());
						classLoaderFrame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
						classLoaderFrame.setVisible(true);
					}
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
			else if (button.getName() == Consts.ApplicationList_CancelButton)
			{
				getView().close();
			}
		}
		if (e.getSource() instanceof JMenuItem)
		{
			JMenuItem item = (JMenuItem) e.getSource();
			if (item.getName() == Consts.ApplicationList_AddAppMenu)
			{
				getView().showFileDialog();
			}
			else if (item.getName() == Consts.ApplicationList_RunAppMenu)
			{
				if (selectedModel != null)
				{
					ClassLoaderFrame classLoaderFrame = new ClassLoaderFrame(selectedModel, getView().getParent(), getView());
					classLoaderFrame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
					classLoaderFrame.setVisible(true);
					selectedModel = null;
				}
			}
			else if (item.getName() == Consts.ApplicationList_RemoveAppMenu)
			{
				if (selectedModel != null)
				{
					appService.removeModel(selectedModel);
					getView().repaint(selectedModel);
					selectedModel = null;
				}
			}
		}
	}

	public void addModel(ClassSettingsModel model)
	{
		appService.addModel(model);
	}

	public List<ClassSettingsModel> getClassSetingsHistory(boolean b)
	{
		return appService.getClassSettingsHistory(b);
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON3)
		{
			if (e.getSource() instanceof JList)
			{
				JList list = (JList) e.getSource();
				int index = list.locationToIndex(e.getPoint());
				boolean isInside = list.getCellBounds(index, index).contains(e.getPoint());
				if (isInside)
					selectedModel = (ClassSettingsModel) list.getModel().getElementAt(index);
				else
					selectedModel = null;
				getView().showPopupMenu(e.getComponent(), e.getX(), e.getY(), isInside);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}
}
